<template>
  <div class="report-list">
    <!-- 顶部工具栏 -->
    <div class="toolbar">
      <el-button type="primary" @click="createReport">
        <i class="fas fa-plus"></i> 新建报表
      </el-button>
      
      <div class="search-box">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索报表"
          prefix-icon="el-icon-search"
          clearable
          @input="handleSearch"
        />
      </div>
    </div>

    <!-- 报表列表 -->
    <div class="list-container">
      <el-table :data="filteredReports" border stripe>
        <el-table-column prop="name" label="报表名称" min-width="200">
          <template #default="{ row }">
            <a href="javascript:;" class="report-name" @click="editReport(row)">
              {{ row.name }}
            </a>
          </template>
        </el-table-column>
        
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        
        <el-table-column prop="updateTime" label="更新时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.updateTime) }}
          </template>
        </el-table-column>
        
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button-group>
              <el-button type="primary" @click="editReport(row)">
                <i class="fas fa-edit"></i> 编辑
              </el-button>
              <el-button type="success" @click="previewReport(row)">
                <i class="fas fa-eye"></i> 预览
              </el-button>
              <el-button type="danger" @click="deleteReport(row)">
                <i class="fas fa-trash"></i> 删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新建报表对话框 -->
    <el-dialog
      title="新建报表"
      v-model="dialogVisible"
      width="400px"
    >
      <el-form
        ref="reportForm"
        :model="reportForm"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="reportForm.name" placeholder="请输入报表名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            type="textarea"
            v-model="reportForm.description"
            placeholder="请输入报表描述"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateReport">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'

const router = useRouter()

// 报表列表数据
const reports = ref([])
const searchKeyword = ref('')
const dialogVisible = ref(false)
const dialogTitle = ref('')
const currentReport = ref(null)

// 表单数据
const reportForm = ref({
  name: '',
  description: ''
})
const reportFormRef = ref(null)

// 表单校验规则
const formRules = {
  name: [
    { required: true, message: '请输入报表名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  description: [
    { max: 200, message: '不能超过200个字符', trigger: 'blur' }
  ]
}

// 过滤后的报表列表
const filteredReports = computed(() => {
  if (!searchKeyword.value) return reports.value
  
  const keyword = searchKeyword.value.toLowerCase()
  return reports.value.filter(report => 
    report.name.toLowerCase().includes(keyword) ||
    report.description?.toLowerCase().includes(keyword)
  )
})

// 加载报表列表
const loadReports = async () => {
  try {
    // 模拟API调用，实际项目中替换为真实API
    reports.value = [
      {
        id: 1,
        name: '示例报表1',
        description: '这是一个示例报表',
        createTime: '2024-01-15T10:00:00',
        updateTime: '2024-01-15T10:00:00'
      }
    ]
  } catch (error) {
    ElMessage.error('加载报表列表失败')
  }
}

// 打开新建报表对话框
const createReport = () => {
  reportForm.value = {
    name: '',
    description: ''
  }
  dialogVisible.value = true
}

// 编辑报表
const editReport = (report) => {
  router.push({
    path: `/report/edit/${report.id}`
  })
}

// 预览报表
const previewReport = (report) => {
  router.push({
    name: 'ReportPreview',
    params: { id: report.id },
    replace: true
  })
}

// 删除报表
const deleteReport = async (report) => {
  try {
    await ElMessageBox.confirm('确定要删除该报表吗？', '提示', {
      type: 'warning'
    })
    
    // 模拟API调用
    reports.value = reports.value.filter(r => r.id !== report.id)
    ElMessage.success('删除成功')
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 处理新建报表
const handleCreateReport = async () => {
  if (!reportForm.value.name) {
    ElMessage.warning('请输入报表名称')
    return
  }

  try {
    // 模拟API调用
    const newReport = {
      id: Date.now(),
      name: reportForm.value.name,
      description: reportForm.value.description,
      components: [],
      createTime: new Date().toISOString(),
      updateTime: new Date().toISOString()
    }
    
    reports.value.push(newReport)
    ElMessage.success('创建成功')
    dialogVisible.value = false

    // 跳转到编辑页面
    router.push({
      path: `/report/edit/${newReport.id}`
    })
  } catch (error) {
    console.error(error)
    ElMessage.error('创建失败')
  }
}

// 格式化日期
const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

// 搜索处理
const handleSearch = () => {
  // 可以添加防抖处理
}

// 初始化加载
onMounted(() => {
  loadReports()
})
</script>

<style scoped>
.report-list {
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: var(--theme-bg, #f8fafc);
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-box {
  width: 300px;
}

.list-container {
  flex: 1;
  background-color: var(--theme-container-bg, #ffffff);
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.report-name {
  color: var(--theme-primary-color, #3b82f6);
  text-decoration: none;
}

.report-name:hover {
  text-decoration: underline;
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
  .list-container {
    background-color: var(--theme-container-bg, #1e293b);
  }
}
</style> 